package com.aaa.controller;

import com.aaa.entity.*;
import com.aaa.service.MenuService;
import com.aaa.service.impl.MenuServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import java.awt.print.Book;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MenuController
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/9/3 22:41
 * @Version:1.0
 **/
@RestController
@RequestMapping("menu")
public class MenuController {
    @Resource
    MenuServiceImpl menuService;
//    menu增删改查
    @RequestMapping("finAll")
    public PageInfo finAll(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(menuService.finAll());
        return pageInfo;
    }
//    设置角色权限
    @RequestMapping("quanxian")
    public List<Menu> finAll(){
        return menuService.menuSelect();
    }
    @RequestMapping("allMid")
    public List<Integer> allMid(Integer rid){
        return menuService.allMid(rid);
    }
    @RequestMapping("allRname")
    public List<Menu> allRname(){
        List<Menu> list = menuService.finByMnane();
        System.out.println(list);
        return list;
    }
    @RequestMapping("add")
    public boolean add(Menu menu){
        QueryWrapper<Menu> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("mname",menu.getMname());
        List<Map<String, Object>> list1 = menuService.listMaps(objectQueryWrapper);
        if(list1.size()>0){
            return false;
        }
        if(menu.getGrade()=="0"){
            menu.setMurl("admin");
        }
        menu.setState(0);
        return menuService.save(menu);
    }
    @RequestMapping("update")
    public boolean update(Menu menu){
        List<Map<String,Object>> list = menuService.addAfter(menu.getMname());
        System.out.println(list);
        if(list.size()>0){
            Map<String,Object> list1 = menuService.addBfore(menu.getMid()).get(0);
            if(list1.get("mname").equals(menu.getMname())){
                return menuService.updateById(menu);
            }
            return false;
        }else {
            return menuService.updateById(menu);
        }
    }

    @RequestMapping("state")
    public boolean state(Menu menu){
        return menuService.updateById(menu);
    }
}
